package com.anjiplus.template.gaea.business.modules.quartz.trigger.factory;


import com.anjiplus.template.gaea.business.modules.quartz.exception.TimingException;
import com.anjiplus.template.gaea.business.modules.quartz.mode.IntervalTimingMode;
import com.anjiplus.template.gaea.business.modules.quartz.mode.TimingModel;
import com.anjiplus.template.gaea.business.modules.quartz.mode.TriggerType;
import com.anjiplus.template.gaea.business.modules.quartz.trigger.ITriggerFactory;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.SimpleTrigger;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.springframework.stereotype.Component;

/**
 * @version 1.0.0
 * @ClassName IntervalTrigger.java
 * @Description
 */
@Component
public class IntervalTrigger implements ITriggerFactory {
    @Override
    public boolean check(TriggerType triggerType) {
        return triggerType == TriggerType.INTERVAL_MINUTE || triggerType == TriggerType.INTERVAL_SECOND || triggerType == TriggerType.INTERVAL_MILLISECOND||triggerType == TriggerType.INTERVAL_HOUR;
    }

    @Override
    public Trigger build(TimingModel timingModel) {
        if (!(timingModel instanceof IntervalTimingMode)){
            throw new TimingException("构建类型为INTERVAL定时必须传入IntervalTimingMode.class的实现类");
        }
        //创建触发器
        SimpleScheduleBuilder simpleScheduleBuilder = SimpleScheduleBuilder.simpleSchedule();
        Long interval = ((IntervalTimingMode) timingModel).getInterval();
        Integer repeatCount = ((IntervalTimingMode) timingModel).getRepeatCount();
        switch (timingModel.getType()){
            case INTERVAL_MINUTE:
                simpleScheduleBuilder.withIntervalInMinutes(Math.toIntExact(interval));
                break;
            case INTERVAL_HOUR:
                simpleScheduleBuilder.withIntervalInHours(Math.toIntExact(interval));
                break;
            case INTERVAL_SECOND:
                simpleScheduleBuilder.withIntervalInSeconds(Math.toIntExact(interval));
                break;
            case INTERVAL_MILLISECOND:
                simpleScheduleBuilder.withIntervalInMilliseconds(interval);
                break;
        }
        // 重复次数，为空则无限重复
        if (repeatCount==null){
            simpleScheduleBuilder.repeatForever();
        }else {
            simpleScheduleBuilder.withRepeatCount(repeatCount);
        }

        TriggerBuilder<SimpleTrigger> simpleTriggerTriggerBuilder = TriggerBuilder.newTrigger().withIdentity(timingModel.getTaskName(), timingModel.getGroupName())
                .withSchedule(simpleScheduleBuilder);
        // 开始结束时间
        if (timingModel.getStartTime()!=null){
            simpleTriggerTriggerBuilder.startAt(timingModel.getStartTime());
        }
        if (timingModel.getEndTime()!=null){
            simpleTriggerTriggerBuilder.endAt(timingModel.getEndTime());
        }
        // 排除日期
//        simpleTriggerTriggerBuilder.modifiedByCalendar("annualCalendar");
        return simpleTriggerTriggerBuilder.build();
    }
}
